myApp.controller('monthlySalesReport', ['$scope', '$http', function($scope, $http){

  $scope.year = "";
  var amountsData = [];
  $scope.searchMonthlySalesReport = function() {

    if($scope.year === "" || $scope.year === null) {
      alert("请填写年份！");
      return;
    }

    var date = {"year": $scope.year};

    var postCfg = {
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      transformRequest: function (data) {
        return $.param(data);
      }
    };

    $http.post('searchMonthlySalesReport', date, postCfg).then(function successCallback(response) {
      amountsData = response.data[0].amounts.split(",");
      showReport();
      console.log("ok：" + response);
    }, function errorCallback(response) {
      alert("查询操作失败!");
      console.log("fail：" + response);
    });


  };

  function showReport() {

    var lineChartData = {
      labels: ["一月", "二月", "三月", "四月", "五月", "六月", "七月","八月", "九月", "十月", "十一月", "十二月"],
      datasets: [{
        label: "Electronics",
        fillColor: "rgba(210, 214, 222, 1)",
        strokeColor: "#7D56F6",
        pointColor: "rgba(210, 214, 222, 1)",
        pointStrokeColor: "#c1c7d1",
        pointHighlightFill: "#fff",
        pointHighlightStroke: "rgba(220,220,220,1)",
        data: amountsData
      }]
    };

    var lineChartOptions = {
      //Boolean - If we should show the scale at all   比例
      showScale: true,
      //Boolean - Whether grid lines are shown across the chart
      scaleShowGridLines: false,
      //String - Colour of the grid lines
      scaleGridLineColor: "rgba(0,0,0,.05)",
      //Number - Width of the grid lines
      scaleGridLineWidth: 1,
      //Boolean - Whether to show horizontal lines (except X axis)
      scaleShowHorizontalLines: true,
      //Boolean - Whether to show vertical lines (except Y axis)
      scaleShowVerticalLines: true,
      //Boolean - Whether the line is curved between points
      bezierCurve: true,
      //Number - Tension of the bezier curve between points
      bezierCurveTension: 0.3,
      //Boolean - Whether to show a dot for each point
      pointDot: false,
      //Number - Radius of each point dot in pixels
      pointDotRadius: 4,
      //Number - Pixel width of point dot stroke
      pointDotStrokeWidth: 1,
      //Number - amount extra to add to the radius to cater for hit detection outside the drawn point
      pointHitDetectionRadius: 20,
      //Boolean - Whether to show a stroke for datasets
      datasetStroke: true,
      //Number - Pixel width of dataset stroke
      datasetStrokeWidth: 2,
      //Boolean - Whether to fill the dataset with a color
      datasetFill: true,
      //String - A legend template
      legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].lineColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>",
      //Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
      maintainAspectRatio: true,
      //Boolean - whether to make the chart responsive to window resizing
      responsive: true
    };

    //-------------
    //- LINE CHART -
    //--------------
    $('.chart').empty();
    $('.chart').append("<canvas id='lineChart'></canvas>");
    var lineChartCanvas = $("#lineChart").get(0).getContext("2d");
    var lineChart = new Chart(lineChartCanvas);
    lineChartOptions.datasetFill = false;
    lineChart.Line(lineChartData, lineChartOptions);

  }


}]);



